﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using InformationSystem.Entity;
using InformationSystem.Service;
using System.Collections;


namespace InformationSystem
{
	/// <summary>
	/// Description of AddProgramme.
	/// </summary>
	public partial class AddProgramme : Form
	{
        ProgrammeService programmeService;

        Programme programme = null;

        ModuleService moduleService = new ModuleService();

        private void initDropdown()
        {
            ArrayList items = new ArrayList();

            items.Add(new DropdownElement(ISUtils.getInstance().PROGRAMME_STATUS_START, "create"));
            items.Add(new DropdownElement(ISUtils.getInstance().PROGRAMME_STATUS_ACTIVE, "in use"));
            items.Add(new DropdownElement(ISUtils.getInstance().PROGRAMME_STATUS_END, "retiring"));

            cbStatus.DisplayMember = "name";
            cbStatus.ValueMember = "id";
            cbStatus.DataSource = items;
            
        }


        private void initListView()
        {
            List<Module> moduleList = null;

            if (null == programme)
            {
                moduleList = moduleService.queryAll();

                for (int i = 0; i < moduleList.Count; i++)
                {
                    Module m = moduleList[i];

                    ListViewItem lvi = new ListViewItem();

                    lvi.Text = m.name + "("+m.credit+")";
                    lvi.Tag = m.id;

                    lvModule.Items.Add(lvi);
                }
            }
            else
            {
                moduleList = moduleService.queryNotSelected(programme.id);

                for (int i = 0; i < moduleList.Count; i++)
                {
                    Module m = moduleList[i];

                    ListViewItem lvi = new ListViewItem();

                    lvi.Text = m.name + "(" + m.credit + ")";
                    lvi.Tag = m.id;

                    lvModule.Items.Add(lvi);
                }

                List<Module> moduleSelectedList = moduleService.querySelected(programme.id);

                for (int i = 0; i < moduleSelectedList.Count; i++)
                {
                    Module m = moduleSelectedList[i];

                    ListViewItem lvi = new ListViewItem();

                    lvi.Text = m.name + "(" + m.credit + ")";
                    lvi.Tag = m.id;

                    lvSelected.Items.Add(lvi);
                }

            }


        }

        public AddProgramme(ProgrammeService _programmeService)
		{			
			InitializeComponent();

            programmeService = _programmeService;

            initDropdown();

            this.Text = "Add Programme";

            initListView();
		}


        public AddProgramme(ProgrammeService _programmeService, int id)
        {
            InitializeComponent();

            programmeService = _programmeService;

            initDropdown();

            programme = new Programme();
            programme.id = id;

            programme = programmeService.query(programme);

            tbName.Text = programme.name;
            cbLevel.SelectedIndex = programme.level - 4;

            cbStatus.SelectedIndex = programme.status - 1;

            this.Text = "Modify Programme";

            initListView();

        }


        private void btSave_Click(object sender, EventArgs e)
        {
            if (tbName.Text == "")
            {
                MessageBox.Show("Please input name");
                return;
            }

            if (cbStatus.SelectedIndex < 0)
            {
                MessageBox.Show("Please choose status");
                return;
            }

            if (cbLevel.SelectedIndex < 0)
            {
                MessageBox.Show("Please choose level");
                return;
            }

            if (tbYear.Text == "")
            {
                MessageBox.Show("Please input year");
                return;
            }

            
            if (programme == null)
            {
                programme = new Programme();
            }
            programme.name = tbName.Text;
            programme.status = (Int32)cbStatus.SelectedValue;
            programme.level = Int32.Parse(cbLevel.Items[cbLevel.SelectedIndex].ToString());
            programme.year = tbYear.Text;
            
            int totalCredit = 0;

            List<int> moduleIds = new List<int>();

            for (int i = 0; i < lvSelected.Items.Count; i++)
            {
                ListViewItem lvi = lvSelected.Items[i];

                string t = lvi.Text;

                string c = t.Substring(t.LastIndexOf("(")+1,t.LastIndexOf(")") - t.LastIndexOf("(")-1);

                int credit = Int32.Parse(c);

                totalCredit += credit;

                moduleIds.Add((int)lvi.Tag);

            }

            programme.moduleIds = moduleIds;

            if (totalCredit > 120)
            {
                MessageBox.Show("total credit for the programme exceed 120 ");
            }

            if (programmeService.addOrUpdate(programme))
            {
                MessageBox.Show("save successful");
            }
            else
            {
                MessageBox.Show("save failed");
            }
        }

        private void btClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btSelected_Click(object sender, EventArgs e)
        {
        	if(cbLevel.SelectedIndex < 0)
        	{
        		MessageBox.Show("Please select the level");
        		return;
        	}
        	
        	
        	if (lvModule.SelectedItems.Count < 1)
            {
                MessageBox.Show("please select module in modules");
                return;
            }

        	int level = Int32.Parse(cbLevel.Items[cbLevel.SelectedIndex].ToString());
        	
        	ListViewItem lvi = lvModule.SelectedItems[0];
        	
        	string name = lvi.Text;
        	
        	if(name.Length < 2)
            {
                MessageBox.Show("Module name is too short(less than 2), so cann't add to programme.");
                return;
            }

            String secondChar = name.Substring(1, 1);
            int l = 0;

            if (!Int32.TryParse(secondChar, out l))
            {
                MessageBox.Show("Module name is incorrect(the second character must be numberic), so cann't add to programme.");
                return;
            }

            if (l < 4 || l > 6)
            {
                MessageBox.Show("Module name is incorrect(the second character must be 4 , 5 or 6), so cann't add to programme.");
                return;
            }
            
            if(l != level)
            {
            	MessageBox.Show("Module level is "+l+", is different with Programme level "+level+", so cann't add to programme.");
                return;
            }
            
            
            lvModule.Items.Remove(lvi);
            lvSelected.Items.Add(lvi);
            
            cbLevel.Enabled= false;

        }

        private void btUnselected_Click(object sender, EventArgs e)
        {
            if (lvSelected.SelectedItems.Count < 1)
            {
                MessageBox.Show("please select module in selected modules");
                return;
            }

            ListViewItem lvi = lvSelected.SelectedItems[0];
            lvSelected.Items.Remove(lvi);
            lvModule.Items.Add(lvi);
            
            if(lvSelected.Items.Count < 1)
            {
            	cbLevel.Enabled= true;
            }
        }
	}
}
